﻿// RS232SetDlg.cpp: 实现文件
//

#include "pch.h"
#include "ProjectAA.h"
#include "RS232SetDlg.h"
#include "afxdialogex.h"


// CRS232SetDlg 对话框

IMPLEMENT_DYNAMIC(CRS232SetDlg, CDialogEx)

CRS232SetDlg::CRS232SetDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DIALOG_COM, pParent)
	, m_nLinkFlag(FALSE)
	, m_nPowerCom(0)
	, m_nPowerPort(0)
	, m_fValSet0(0)
	, m_fCurrentSet0(0)
	, m_fValSet1(0)
	, m_fCurrentSet1(0)
	, m_nLightCom(0)
	, m_nLightPort(0)
	, RedCurrentAdd(0)
{

}

CRS232SetDlg::~CRS232SetDlg()
{
}

void CRS232SetDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Check(pDX, IDC_CHECK_POWERLINK, m_nLinkFlag);
	DDX_Text(pDX, IDC_EDIT_COM3, m_nPowerCom);
	DDX_Text(pDX, IDC_EDIT_PORT3, m_nPowerPort);
	DDX_Text(pDX, IDC_EDIT_VAL0, m_fValSet0);
	DDX_Text(pDX, IDC_EDIT_CURRENT0, m_fCurrentSet0);
	DDX_Text(pDX, IDC_EDIT_VAL1, m_fValSet1);
	DDX_Text(pDX, IDC_EDIT_CURRENT1, m_fCurrentSet1);
	DDX_Text(pDX, IDC_EDIT_COM4, m_nLightCom);
	DDX_Text(pDX, IDC_EDIT_PORT4, m_nLightPort);
	DDX_Text(pDX, IDC_EDIT_COM5, RedCurrentAdd);
}


BEGIN_MESSAGE_MAP(CRS232SetDlg, CDialogEx)
	ON_BN_CLICKED(IDC_BUTTON_SAVE, &CRS232SetDlg::OnBnClickedButtonSave)
END_MESSAGE_MAP()


// CRS232SetDlg 消息处理程序


BOOL CRS232SetDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	ReadConfigIni();

	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}


void CRS232SetDlg::OnCancel()
{
	// TODO: 在此添加专用代码和/或调用基类

	CDialogEx::OnCancel();
}

void CRS232SetDlg::ReadConfigIni()
{
	m_nLinkFlag = gSystemMgr.m_nPowerFlag;
	m_nPowerCom = gSystemMgr.m_nPowerPort;
	m_nPowerPort = gSystemMgr.m_nPowerBaudrate;
	m_fValSet0 = gSystemMgr.voltage_val[0];
	m_fCurrentSet0 = gSystemMgr.current_limit[0];
	m_fValSet1 = gSystemMgr.voltage_val[1];
	m_fCurrentSet1 = gSystemMgr.current_limit[1];

	m_nLightCom = gSystemMgr.m_nLightPort;
	m_nLightPort = gSystemMgr.m_nLightBaudrate;

	RedCurrentAdd = gSystemMgr.RedCurrentAdd;

	UpdateData(FALSE);
}

void CRS232SetDlg::SaveValueToConfig()
{
	UpdateData(TRUE);

	gSystemMgr.m_nPowerPort = m_nPowerCom;
	gSystemMgr.m_nPowerBaudrate = m_nPowerPort;
	gSystemMgr.m_nPowerFlag = m_nLinkFlag;
	gSystemMgr.current_limit[0] = m_fCurrentSet0;
	gSystemMgr.voltage_val[0] = m_fValSet0;
	gSystemMgr.current_limit[1] = m_fCurrentSet1;
	gSystemMgr.voltage_val[1] = m_fValSet1;

	gSystemMgr.m_nLightPort = m_nLightCom;
	gSystemMgr.m_nLightBaudrate = m_nLightPort;

	gSystemMgr.RedCurrentAdd = RedCurrentAdd;

	CString cstr;
	cstr.Format(L"%.3f", gSystemMgr.RedCurrentAdd);
	gSystemMgr.m_cRS232ConfigIni.SaveConfigIni(L"Power", L"RedCurrentAdd", cstr);

	cstr.Format(L"%d", gSystemMgr.m_nPowerPort);
	gSystemMgr.m_cRS232ConfigIni.SaveConfigIni(L"Power", L"m_nPowerPort", cstr);

	cstr.Format(L"%d", gSystemMgr.m_nPowerBaudrate);
	gSystemMgr.m_cRS232ConfigIni.SaveConfigIni(L"Power", L"m_nPowerBaudrate", cstr);

	cstr.Format(L"%d", gSystemMgr.m_nPowerFlag);
	gSystemMgr.m_cRS232ConfigIni.SaveConfigIni(L"Power", L"m_nPowerFlag", cstr);

	cstr.Format(L"%.2f", gSystemMgr.current_limit[0]);
	gSystemMgr.m_cRS232ConfigIni.SaveConfigIni(L"Power", L"current_limit[0]", cstr);

	cstr.Format(L"%.2f", gSystemMgr.current_limit[1]);
	gSystemMgr.m_cRS232ConfigIni.SaveConfigIni(L"Power", L"current_limit[1]", cstr);

	cstr.Format(L"%.2f", gSystemMgr.voltage_val[0]);
	gSystemMgr.m_cRS232ConfigIni.SaveConfigIni(L"Power", L"voltage_val[0]", cstr);

	cstr.Format(L"%.2f", gSystemMgr.voltage_val[1]);
	gSystemMgr.m_cRS232ConfigIni.SaveConfigIni(L"Power", L"voltage_val[1]", cstr);

	cstr.Format(L"%d", gSystemMgr.m_nLightPort);
	gSystemMgr.m_cRS232ConfigIni.SaveConfigIni(L"Power", L"m_nLightPort", cstr);

	cstr.Format(L"%d", gSystemMgr.m_nLightBaudrate);
	gSystemMgr.m_cRS232ConfigIni.SaveConfigIni(L"Power", L"m_nLightBaudrate", cstr);


	gSystemLog.DisplayLogAndSave(L"RS232保存数据完成", LV_EVENT);
}

void CRS232SetDlg::OnBnClickedButtonSave()
{
	SaveValueToConfig();
}
